<?php

function getAlbums()
{

    global $db;

    $table = 'albums';

    $db->select($table);
    $albums = $db->fetchAll();

    return $albums;
}

function getCleanPics()
{
    global $db;

    $table = 'photographs';

    $db->select($table);
    $photographs = $db->fetchAll();

    return $photographs;
}

function indexOf($index, $array)
{

    $new = array();

    foreach ($array as $arr) {
        $new[$arr[$index]] = $arr;
    }

    return $new;
}

function getPics($album_id = 1)
{

    global $db;

    if ($album_id == 1) {

        $table = 'photographs';

        $db->select($table);
        $pics = $db->fetchAll();
    } else {

        $table = 'photographs';
        $fields = '*';
        $where_sql = 'album_id = :album_id';
        $where_data = array('album_id' => $album_id);

        $db->select($table, $fields, $where_sql, $where_data);
        $pics = $db->fetchAll();
    }


    $albums = getAlbums();
    $albums = indexOf('id', $albums);

    $first = true;
    $hidden = '';


    foreach ($pics as $pic) {

        if ($first != true) {
            $hidden = 'style="display: none;"';
        }

        $first = false;
        $title = $pic['title'];
        $alt = $albums[$pic['album_id']]['alt'];
        $imagePath = ALBUM_PATH . $albums[$pic['album_id']]['name'] . '/' . $pic['source'];


        ?>

    <li <?php echo $hidden; ?> >
        <header style="height: 20px; overflow: hidden;">
            <h2 class="f_left"><?php echo $title; ?><h2>
            <div class="fb-like f_right" style="margin-right: -29px;" data-href="http://www.facebook.com/pages/%D0%97%D0%B0%D0%B1%D0%B0%D0%B2%D0%BB%D1%8F%D0%B2%D0%B0%D0%B9-%D1%81%D0%B5-%D0%B5%D0%B4%D0%B8%D0%BD-%D0%BF%D1%8A%D1%82-%D1%81%D0%B5-%D0%B6%D0%B8%D0%B2%D0%B5%D0%B5/459778490725668" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div>
            <div class="clear"></div>
        </header>
        <table>
            <tr>
                <td style="vertical-align: top">
                    <img src="<?php echo $imagePath; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" style="max-height: 500px;" class="box"/><br/>
                </td>
                <td style="vertical-align: top">

                </td>
            </tr>
        </table>
        <div class="fb-comments" data-href="<?php echo $imagePath; ?>" data-width="318" data-num-posts="1"></div>

    </li>

    <?php

    }
}

function deleteDir($dirPath)
{
    if (!is_dir($dirPath)) {
        throw new InvalidArgumentException("$dirPath must be a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            self::deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}